package atm;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transform extends JPanel implements ActionListener {

    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon transformButton = Util.getIcon("/atmimage/buttonSure.jpg");

    //声明转账金额和转账对象
    private JTextField transformMoney = new JTextField();
    private JTextField transformPerson = new JTextField();
    private JButton transformBtn = new JButton(transformButton);
    private JLabel personLabel = new JLabel("请输入转账对象：");
    private JLabel moneyLabel = new JLabel("请输入转账金额：");


    public Transform(MainPage frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("转账");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        transformPerson.setBounds(300, 150, 200, 25);
        this.add(transformPerson);

        transformMoney.setBounds(300, 200, 200, 25);
        this.add(transformMoney);

        transformBtn.setBounds(350, 330, 100, 50);
        this.add(transformBtn);
        transformBtn.addActionListener(this);

        personLabel.setBounds(170, 150, 150, 25);
        this.add(personLabel);

        moneyLabel.setBounds(170, 200, 150, 25);
        this.add(moneyLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        if (backBtn == e.getSource()) {
//            frame.switchPanel("menu");
//            return;
//        }

//        if (transformPerson.getText().equals(frame.getUserarraylist().get(frame.getIndex()).getAccount())) {
//            JOptionPane.showMessageDialog(this, "不能给自己转账");
//        } else {
//            for (int i = 0; i < frame.getUserarraylist().size(); i++) {
//                if (transformPerson.getText().equals(frame.getUserarraylist().get(i).getAccount()) && Double.valueOf(transformMoney.getText()) < frame.getUserarraylist().get(frame.getIndex()).getMoney()) {
//                    double outMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() - Double.valueOf(transformMoney.getText());
//                    frame.getUserarraylist().get(frame.getIndex()).setMoney(outMoney);
//                    JOptionPane.showMessageDialog(this, outMoney);
//                    double inMoney = frame.getUserarraylist().get(i).getMoney() + Double.valueOf(transformMoney.getText());
//                    frame.getUserarraylist().get(i).setMoney(inMoney);
//                    JOptionPane.showMessageDialog(this, inMoney);
//                }
//            }
//            JOptionPane.showMessageDialog(this, "找不到收款方");
//        }

//        if (transformPerson.getText().equals(frame.getUser().getAccount())) {
//            JOptionPane.showMessageDialog(this, "不能转给自己");
//            return;
//        }
//        if (frame.getUser().getMoney() < Double.valueOf(transformMoney.getText())) {
//            JOptionPane.showMessageDialog(this, "余额不足");
//            return;
//        }
////        System.out.println(frame.getUser().getMoney());
////        System.out.println(Integer.parseInt(transformMoney.getText()));
//        for (int i = 0; i < frame.getUserarraylist().size(); i++) {
//            if (transformPerson.getText().equals(frame.getUserarraylist().get(i).getAccount())) {
//                double outMoney = frame.getUser().getMoney() - Double.valueOf(transformMoney.getText());
//                frame.getUserarraylist().get(frame.getIndex()).setMoney(outMoney);
////                FileUtil.writeObject(MainPage.PATH,frame.getUserarraylist());
//                JOptionPane.showMessageDialog(this, "您当前余额为：" + outMoney);
//                double inMoney = Double.valueOf(transformMoney.getText()) + frame.getUserarraylist().get(i).getMoney();
//                frame.getUserarraylist().get(i).setMoney(inMoney);
//                FileUtil.writeObject(MainPage.PATH,frame.getUserarraylist());
//                transformPerson.setText("");
//                transformMoney.setText("");
//                JOptionPane.showMessageDialog(this, "对方余额为：" + inMoney);
//                return;
//            }
//        }
//        JOptionPane.showMessageDialog(this, "查无此人");


        if(e.getSource() == transformBtn){
            // 获取输入的对方账号
            String otherAccount = transformPerson.getText();
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(transformMoney.getText());
            }catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"转账金额必须为数字，并不能为空");
                return;
            }

            // 当期登录的账户
            SqlUtil db = new SqlUtil();
            User loginUser = db.getById(frame.getUser().getId());
            User otherUser = db.getByUsername(otherAccount);
//            ArrayList<User> users = frame.getUsers();
//
//            boolean isExist = false;
//            for(int i = 0;i < users.size();i++){
//                if(users.get(i).getAccount().equals(otherAccount)){
//                    isExist = true;
//                    otherUser = users.get(i);
//                    break;
//                }
//            }
            // 判断账号是否存在
            if(otherUser == null){
                JOptionPane.showMessageDialog(this,"查无此人");
                return;
            }
            // 判断是否转给自己
            if(loginUser.getAccount().equals(otherAccount)){
                JOptionPane.showMessageDialog(this,"不能转给自己");
                return;
            }
            // 判断余额是否足够
            if(blance > loginUser.getMoney()){
                JOptionPane.showMessageDialog(this,"余额不足");
                return;
            }
            // 当期账户扣钱
            loginUser.setMoney(loginUser.getMoney() - blance);
            // 对方账户加钱
            otherUser.setMoney(otherUser.getMoney() + blance);
            db.update(loginUser,otherUser);
//            db.update(otherUser);
            JOptionPane.showMessageDialog(this,"转账成功");
            frame.switchPanel("menu");
        }else if(e.getSource() == backBtn){
            frame.switchPanel("menu");

        }
    }



}
